<?php
namespace jy_yuyue;
defined('BY_JYA') or exit('error');
/*
*/
class notice extends \plugin{
	public function gettpl_data($orderid,$type=0){//type=0表单订单，type=1
		return false;
	}
	/*
		抢单通知
	*/
	public function notice_ordermsg($order=array(),$allusers=array()){
		//新配送任务通知给配送员
		$goods=pdo_get('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$order['id']));
		$goods['title']=pdo_getcolumn('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$goods['goodsid']),'title');
		$address_str=model('core/member')->get_address_str($order['addressid']);
		$template_id=cfc('account')->get_tpl_template_id('48089','收到客户新订单通知','',array('订单时间','商品名称','客户地址'),'ordermsg','jy_yuyue');
		if($_SESSION['wxapp_id']){
			$wxapptemplate_id=cfc('wxapp_api')->get_tpl_template_id('17468',[1,2,3,7],'ordermsg','收到客户新订单通知','jy_yuyue');
		}
		$goods['title']=cfc('tools')->substr($goods['title'],0,10);
		$address_str=$address_str?:'*** ***';
		$message=array(
			array('value'=>date('Y/m/d H:i:s',$order['createtime']),"name" => "time2"),
			array('value'=>$goods['title'].'等商品',"name" => "thing8"),
			array('value'=>$address_str,"name" => "thing5"),
		);
		$wxappmsg=array(
			array('value'=>$order['ordersn']),
			array('value'=>date('Y/m/d H:i:s',$order['createtime'])),
			array('value'=>$goods['title']),
			array('value'=>$address_str),
		);
		if(!$url){
			$url=custom_url('jy_yuyue/peisong_order/',true);	
		}
		foreach($allusers as $uv){
			cfc('account')->sendtwoNotice($uv['mid'],$template_id,$message,$url,'ordermsg','jy_yuyue',$order['id']);
			if($_SESSION['wxapp_id']){
				$url='/jy_yuyue/peisong_order/';
				$template_id=cfc('wxapp_api')->checktplNotice($uv['mid'],$wxapptemplate_id,$wxappmsg,$url,'ordermsg','jy_yuyue',$order['id']);
			}
		}
		return '';
	}
	/*
		开启预约通知员工消息模版
	*/
	public function notice_workermsg($order=array(),$isexi=false){
		$serve=pdo_get('jy_yuyue_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['serve_id']));
		$user=pdo_get('jy_yuyue_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['user_id']));
		$worker=pdo_get('jy_yuyue_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['worker_id']));
		$serve_times=pdo_getcolumn('jy_yuyue_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['serve_id']),'serve_time');
		$serve_times=json_decode($serve_times,true);
		$serve_time=$serve_times[$order['timeid']];
		$address_str=$order['address']?:'*** ***';
		$template_id=cfc('account')->get_tpl_template_id('48089','收到客户新订单通知',$isexi,array('订单时间','商品名称','客户地址'),'ordermsg','jy_yuyue');
		if($_SESSION['wxapp_id']){
			$wxapptemplate_id=cfc('wxapp_api')->get_tpl_template_id('17468',[1,2,3,7],'ordermsg','收到客户新订单通知','jy_yuyue');
		}
		$serve['title']=cfc('tools')->substr($serve['title'],0,10);
		$message=array(
			array('value'=>date('Y/m/d H:i:s',$order['createtime']),"name" => "time2"),
			array('value'=>$serve['title'],"name" => "thing8"),
			array('value'=>$address_str,"name" => "thing5"),
		);
		$wxappmsg=array(
			array('value'=>$order['ordersn']),
			array('value'=>date('Y/m/d H:i:s',$order['createtime'])),
			array('value'=>$serve['title']),
			array('value'=>$address_str),
		);
		if(!$url){
			$url=custom_url('jy_yuyue/home/',true);	
		}
		cfc('account')->sendtwoNotice($worker['mid'],$template_id,$message,$url,'ordermsg','jy_yuyue',$order['id'],$isexi);
		if($_SESSION['wxapp_id']){
			$url='/jy_yuyue/home/';
			$template_id=cfc('wxapp_api')->checktplNotice($worker['mid'],$wxapptemplate_id,$wxappmsg,$url,'ordermsg','jy_yuyue',$order['id']);
		}
		return '';
	}
	/*
		开启预约已确认通知消息模版
	*/
	public function notice_touser($order_id,$desc=''){
		//新配送任务通知给配送员
		$order=pdo_get('jy_yuyue_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order_id));
		$serve=pdo_get('jy_yuyue_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['serve_id']));
		$serve_times=pdo_getcolumn('jy_yuyue_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['serve_id']),'serve_time');
		$serve_times=json_decode($serve_times,true);
		$serve_time=$serve_times[$order['timeid']];
		$desc=$desc?:$serve['title'].'-'.$order['day'].'--'.$serve_time['times']['start'].'-'.$serve_time['times']['end'];
		$template_id=cfc('account')->get_tpl_template_id('53653','订单派送通知','',array('订单编号','商品','配送地址'),'orderget','jy_yuyue');
		if($_SESSION['wxapp_id']){
			$wxapptemplate_id=cfc('wxapp_api')->get_tpl_template_id('17468',[1,2,3,7],'orderget','新配送任务通知','jy_yuyue');
		}
		$address_str=$order['address']?:'无地址';
		$desc=cfc('tools')->substr($desc,0,10);
		$message=array(
			array('value'=>$order['ordersn'],"name" => "character_string7"),
			array('value'=>$desc,"name" => "thing6"),
			array('value'=>$address_str,"name" => "thing3"),
		);
		$wxappmsg=array(
			array('value'=>$order['ordersn']),
			array('value'=>date('Y/m/d H:i:s',$order['createtime'])),
			array('value'=>$desc),
			array('value'=>$address_str),
		);
		if(!$url){
			$url=custom_url('jy_yuyue/home/',true);	
		}
		if(method_exists(cfc('account'),'sendtwoNotice')){
			cfc('account')->sendtwoNotice($order['mid'],$template_id,$message,$url,'orderget','jy_yuyue',$order_id);
		}
		if($_SESSION['wxapp_id']){
			$url='/jy_yuyue/home/index/index';
			$template_id=cfc('wxapp_api')->checktplNotice($order['mid'],$wxapptemplate_id,$wxappmsg,$url,'orderget','jy_yuyue',$order_id);
		}
	}
	/*
		配送员新任务通知
	*/
	public function notice_orderget($order_id,$mid='',$isexi=false){
		//新配送任务通知给配送员
		$order=pdo_get('jy_yuyue_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order_id));
		$serve=pdo_get('jy_yuyue_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['serve_id']));
		if(!$mid){
			$worker=pdo_get('jy_yuyue_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['worker_id']));
			$mid=$worker['mid'];
		}
		$serve_times=pdo_getcolumn('jy_yuyue_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['serve_id']),'serve_time');
		$serve_times=json_decode($serve_times,true);
		$serve_time=$serve_times[$order['timeid']];
		$desc=$serve['title'].'-'.$order['day'].'--'.$serve_time['times']['start'].'-'.$serve_time['times']['end'];
		$template_id=cfc('account')->get_tpl_template_id('53653','订单派送通知','',array('订单编号','商品','配送地址'),'orderget','jy_yuyue');
		if($_SESSION['wxapp_id']){
			$wxapptemplate_id=cfc('wxapp_api')->get_tpl_template_id('17468',[1,2,3,7],'orderget','新配送任务通知','jy_yuyue');
		}
		$address_str=$order['address']?:'无地址';
		$desc=cfc('tools')->substr($desc,0,10);
		$message=array(
			array('value'=>$order['ordersn'],"name" => "character_string7"),
			array('value'=>$desc,"name" => "thing6"),
			array('value'=>$address_str,"name" => "thing3"),
		);
		$wxappmsg=array(
			array('value'=>$order['ordersn']),
			array('value'=>date('Y/m/d H:i:s',$order['createtime'])),
			array('value'=>$desc),
			array('value'=>$address_str),
		);
		if(!$url){
			$url=custom_url('jy_yuyue/worker_order/',true);	
		}
		cfc('account')->sendtwoNotice($mid,$template_id,$message,$url,'orderget','jy_yuyue',$order_id.$isexi);
		if($_SESSION['wxapp_id']){
			$url='/jy_yuyue/worker_order/index/index';
			$template_id=cfc('wxapp_api')->checktplNotice($mid,$wxapptemplate_id,$wxappmsg,$url,'orderget','jy_yuyue',$order_id);
		}
	}
	public function notice_check_apply($apply,$url=''){
		//审核通过
		$member=$this->get('member',array('id'=>$apply['pid'],'uniacid'=>$_SESSION['uniacid']));
		$template_id=cfc('account')->get_tpl_template_id('OPENTM408250107','申请成功通知');
		$message=array(
			'first'=>array('value'=>'申请成为配送员，审核通过！',"color" => "#4a5077"),
			'keyword1'=>array('value'=>$member['title'],"color" => "#4a5077"),
			'keyword2'=>array('value'=>$member['mobile'],"color" => "#4a5077"),
			'remark'=>array('value'=>'感谢你的使用。',"color" => "#4a5077"),
		);
		if(!$url){
			$url=custom_url('jy_yuyue/index/',true);	
		}
		if(method_exists(cfc('account'),'sendtwoNotice')){
			cfc('account')->sendtwoNotice($member['mid'],$template_id,$message,$url,'check_apply','jy_yuyue',$order_id);
		}
	}
}